<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>Gestalt Sample: Simple Image Sequencer - Python</title>

    <script src="../../js/jquery.js" type="text/javascript"></script>

    <script src="../../js/gestalt.js" type="text/javascript"></script>

</head>
<body>

<script language="python">

# imports
from System import * # for Uri
from System.Windows.Shapes import * # exposes Rectangle to scope since not added by default
from System.Windows.Media.Imaging import * # for bitmap
from System.Diagnostics import * # enables outputing to a debug window!

#localize
grid = me.grid
layoutRoot = me.LayoutRoot
bkg = me.bkg
frame = me.frame
holder = me.holder

totalImages = 37
imageWidth = 150
startAngle = 0
isDrag = False
pointer = 0
calcPointer = 0

#
# Define event handlers before they are referenced below
# probably necessary since it is a one pass compile
#
def bkg_MouseLeftButtonDown(sender, e):
    global isDrag # if referencing a global variable, it must be explicity stated
    x = e.GetPosition(layoutRoot).X - 300
    y = -1 * (e.GetPosition(layoutRoot).Y - 150)
    startAngle = Math.Atan2(y, x) / (Math.PI / 180)
    if (startAngle < 0):
        startAngle += 360
    if (startAngle > 360):
        startAngle = (startAngle % 360)
    isDrag = True
    bkg.CaptureMouse()

def bkg_MouseLeftButtonUp(sender, e):
    global isDrag
    isDrag = False
    bkg.ReleaseMouseCapture()
    pointer = calcPointer

def bkg_MouseMove(sender, e):
    global isDrag
    if (isDrag == False):
        return
    mouse = e.GetPosition(layoutRoot)
    _x = mouse.X - 300
    _y = -1 * (mouse.Y - 150)
    targetAngle = Math.Atan2(_y, _x) / (Math.PI / 180)
    if (targetAngle < 0):
        targetAngle += 360
    if (targetAngle > 360):
        targetAngle = (targetAngle % 360)
    delta = targetAngle - startAngle
    frameOffset = Math.Floor((delta / 360) * totalImages)
    calcPointer = pointer + frameOffset
    if (calcPointer < 0):
        calcPointer += totalImages
    if (calcPointer >= totalImages): 
        calcPointer -= totalImages
    holder.SetValue(Canvas.LeftProperty, calcPointer * -imageWidth)

#
# attach the handlers
#
bkg.MouseLeftButtonDown += bkg_MouseLeftButtonDown
bkg.MouseLeftButtonUp += bkg_MouseLeftButtonUp
bkg.MouseMove += bkg_MouseMove

#
# load images
#
for i in range(0, totalImages):
    # this path might seem odd because the images need to be found relative
    # to the /gs directory. Alternately you could specify a fully qualified path to images
    imageName = "../samples/image.sequencer/images/redGlobe_" + i.ToString() + ".png"
    img = Image()
    img.Source = BitmapImage(Uri(imageName, UriKind.Relative))
    img.SetValue(Canvas.LeftProperty, Convert.ToDouble(i * imageWidth))
    holder.Children.Add(img)

</script>

    <!-- This isn't a part of the sample, but a link back to the Gestalt site -->
    <div id="upsell" style="color:#fff; font-size: 13px; font-family:'Lucida Grande','Lucida Sans Unicode',Helvetica,Arial,Lucida,Verdana,sans-serif; margin-bottom: 30px;">
        <a href="http://www.visitmix.com/labs/gestalt/samples/" style="background-color: #000; color: #fff; display: block; text-decoration: none;" onmouseover="this.style.color='#000'; this.style.background='#ccc'" onmouseout="this.style.color='#fff'; this.style.background='#000'">
		    <span style="display:block; padding: 8px; text-align:center;">&laquo; Check out other samples on the Gestalt web site</span>
	    </a>
    </div>

<!-- The following commented line of code is equivalent to the inline XAML
     and we recommend doing it this way. Only caveat, if you link to a XAML
     file, you'll be able to view the page correctly only through a server 
     (e.g. apache or IIS) because of security sandbox reasons. -->
    
    <xml id="TODO" class="xaml" width="600" height="300">
        <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  x:Name="grid" Width="600" Height="300" Background="Tan">
        
            <Canvas x:Name="LayoutRoot">
                <Canvas.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Color="#000000" Offset="0.4"></GradientStop>
                        <GradientStop Color="#FF0000" Offset="1.0"></GradientStop>
                    </LinearGradientBrush>
                </Canvas.Background>
                <Ellipse x:Name="bkg" Width="260" Height="260" Canvas.Left="170" Canvas.Top="20" Cursor="Hand">
                    <Ellipse.Fill>
                        <RadialGradientBrush>
                            <GradientStop Color="#00FF0000" Offset=".6"></GradientStop>
                            <GradientStop Color="#66FF0000" Offset="1.0"></GradientStop>
                        </RadialGradientBrush>
                    </Ellipse.Fill>
                </Ellipse>
                <Canvas x:Name="frame" Width="150" Height="140" Canvas.Left="225" Canvas.Top="80" IsHitTestVisible="False">
                    <Canvas.Clip>
                        <RectangleGeometry Rect="0,0,150,140"></RectangleGeometry>
                    </Canvas.Clip>
                    <Canvas x:Name="holder"></Canvas>
                </Canvas>
            </Canvas>
		    
	    </Grid>
    </xml>
    
</body>
</html>
